<template>
	<view>
		<u-toast ref="uToast" />
		<u-no-network></u-no-network>
		<u-navbar title="地址列表" safeAreaInsetTop fixed placeholder>
			<view class="coreshop-navbar-left-slot" slot="left">
				<u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
				<u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
				<u-icon name="home" size="22" @click="goHome"></u-icon>
			</view>
			<view slot="right">
			</view>
		</u-navbar>
		<view class="content">
			<view v-if="list.length">
				<view class="item  coreshop-display-flex coreshop-flex-nowrap" v-for="(item, key) in list" :key="key">
					<view @click="isSelect(item)">
						<view class="top">
							<view class="name">{{ item.name }}</view>
							<view class="phone">{{ item.mobile }}</view>
							<view class="tag">
								<text class="red" v-if="item.isDefault">默认</text>
							</view>
						</view>
						<view class="bottom">
							{{item.areaName }} {{item.street}} {{item.address}}
						</view>
					</view>
					<u-icon name="edit-pen-fill" :size="20" color="#999999" v-show="type != 'order'"
						@click="toEdit(item.id)"></u-icon>
				</view>
			</view>
			<!-- 无数据时默认显示 -->
			<view class="coreshop-emptybox" v-else>
				<u-empty :icon="$globalConstVars.apiFilesUrl+'/static/images/empty/address.png'" icon-size="150"
					text="暂无地址信息" mode="list"></u-empty>
			</view>

			<!--按钮-->
			<view class="coreshop-bg-white coreshop-footer-fixed coreshop-foot-padding-bottom">
				<u-button class="coreshop-percent-100" type="error" size="normal" @click="toAdd">新增收货地址</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [], // 用户收货地址列表
				type: ''
			}
		},
		onLoad(e) {
			if (e.type) {
				this.type = e.type;
			}
		},
		onShow() {
			this.userShipList();
		},
		methods: {
			// 获取收货地址列表
			userShipList() {
				this.$u.api.userShip().then(res => {
					if (res.status) {
						this.list = res.data
					}
				})
			},
			//编辑
			toEdit(id) {
				this.$u.route('/pages/member/address/index/index?shipId=' + id);
			},
			//添加
			toAdd() {
				this.$u.route('/pages/member/address/index/index');
			},
			//选择
			isSelect(data) {
				if (this.type == 'order') {
					let pages = getCurrentPages(); //当前页
					let beforePage = pages[pages.length - 2]; //上个页面

					// #ifdef MP-ALIPAY || MP-TOUTIAO
					this.$db.set('addressUserShip', data, true);
					// #endif

					// #ifdef APP-PLUS || APP-PLUS-NVUE
					this.$store.commit("userShip", data)
					// #endif

					// #ifdef MP-WEIXIN
					beforePage.$vm.userShip = data;
					beforePage.$vm.params.userShipId = data.id;
					beforePage.$vm.params.areaId = data.areaId;
					// #endif

					uni.navigateBack({
						delta: 1
					});
					// this.$u.route("/pages/placeOrder/index/index")
				}
			},
			// #ifdef MP-WEIXIN
			wechatAddress: function() {
				let _that = this;
				wx.chooseAddress({
					success: res => {
						if (res.errMsg == "chooseAddress:ok") {
							//获取成功
							//存储这个收获地区信息到数据库
							let data = {
								provinceName: res.provinceName,
								cityName: res.cityName,
								countyName: res.countyName,
								postalCode: res.postalCode
							};
							let areaId = 0;
							this.$u.api.getAreaId(data).then(res1 => {
								if (res1.status) {
									//存储用户收货信息
									let userShipId = 0;
									let userShipData = {
										areaId: res1.data,
										name: res.userName,
										address: res.detailInfo,
										mobile: res.telNumber,
										isDefault: 2
									}
									this.$u.api.saveUserShipWx(userShipData).then(res2 => {
										if (res2.status) {
											this.$refs.uToast.show({
												title: '存储微信地址成功',
												type: 'success',
												complete: function() {
													_that.userShipList();
												}
											});
										} else {
											this.$refs.uToast.show({
												message: '存储微信地址失败',
												type: 'error'
											});
										}
									});
								} else {
									this.$refs.uToast.show({
										message: '获取微信地址失败',
										type: 'error'
									});
								}
							});
						} else {
							this.$refs.uToast.show({
								message: '获取微信地址失败',
								type: 'error'
							});
						}
					}
				});
			},
			// #endif
		}
	}
</script>

<style lang="scss" scoped>
	@import "list.scss";
</style>
